//记录编辑时调取代码编辑器
if(typeof CodeMirror!="undefined"){
    var mode = $(".save").attr("data-mode");
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: mode,
        lineNumbers: true,
        lineWrapping: true,
    });
}

//默认加载列表
if ($("#rows").length > 0) {
    loadingData();
}

$(".dropdown-item").bind('click', function() {
    $("#ext-show").text($(this).text());
    $("#ext").val($(this).text());
});

//创建记录
$('#create-bt').on('click', function (event) {
    var siteid = $("#rows").attr("data-siteid");
    $.ajax({
        type: "post",
        dataType: "json",
        url: "/"+siteid+"/asset/create",
        data:{ext:$("#ext").val(),path:$("#path").val()},
        success: function (result) {
            if (result.code == 0) {
                $("#ext").val("");
                $("#path").val("");
                $("#myModal").modal('hide');
                bootoast({
                    message: '生成成功',
                    type: 'success',
                    position:'right-bottom',
                    timeout:2
                });
                loadingData();
            } else {
                bootoast({
                    message: '创建失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error: function() {
            bootoast({
                message: '接口异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
    return false;
})

//保存更改
$(".save").bind('click', function() {
    var obj = $(this);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/"+$(this).attr("data-siteid")+"/asset/save",
        data: {content:editor.getValue(),fn:$(this).attr("data-fn")},
        success: function (result) {
            if (result.code == 0) {
                obj.html('<span class="glyphicon glyphicon-floppy-saved" aria-hidden="true" ></span> 保存成功');
                setTimeout(function(){
                    obj.html('<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true" ></span> 保存更改');
                },2000)
            } else {
                obj.html('<span class="glyphicon glyphicon-floppy-remove" aria-hidden="true" ></span> 保存失败');
                setTimeout(function(){
                    obj.html('<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true" ></span> 保存更改');
                },2000)
                bootoast({
                    message: '保存失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error : function() {
            bootoast({
                message: '异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
});

//删除记录
function deleteRows(ids)
{
    $.ajax({
        type: "post",
        dataType: "json",
        url: "/"+$("#rows").attr("data-siteid")+"/asset/delete",
        data:{ids:ids},
        success: function (result) {
            if (result.code == 0) {
                for (var key in result.data) {
                    $("#tr-"+result.data[key]).remove();
                }
                bootoast({
                    message: '删除成功，'+result.msg,
                    type: 'success',
                    position:'right-bottom',
                    timeout:2
                });
            } else {
                bootoast({
                    message: '删除失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error : function() {
            bootoast({
                message: '接口异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
}

//读取记录列表
function loadingData(page = 1)
{
    var siteid = $("#uploader").attr("data-siteid");
    $.ajax({
      type : "get",
      url: "/"+siteid+"/asset/rows?page="+page,
      beforeSend:function(XMLHttpRequest){
        if (page == 1) {
            $("#rows").html('<div class="text-center"><img src=\"/static/common/img/loading.gif\" width="48"></div>');
        }
      },
      success: function (result) {
        if (page == 1) {
            $("#rows").empty();
        }

        if(result.current == 0 && page == 1){
            $("#rows").append('<div class="well well-lg text-center">哇偶！ 下空空如也，啥也没有。</div>');
        } else {
            if (page > 1) {
                var html = '';
                html += loopRows(siteid, result.data);
                $("#tb").append(html);
                if (result.pagesize > result.current || page == result.total) {
                    $("#more").hide();
                }
            } else {
                var html = '<table class="table table-hover"><thead><tr><th width="5%"><div class="checkbox checkbox-primary checkbox-single m-r-15"><input id="action-checkbox" title="全选" name="all" type="checkbox"><label for="action-checkbox"></label></div></th><th>文件名</th><th width="10%">类型</th><th width="12%">大小</th><th width="16%">修改时间</th><th width="15%">操作</th></tr></thead><tbody id="tb">';
                    html += loopRows(siteid, result.data);
                html += '</tbody></table>';
                if (result.pagesize == result.current) {
                    html += '<div class="text-center"><a href="javascript:;" id="more" data-page="2">加载更多...</a></div>';
                }
                $("#rows").append(html);
            }
        }
      },
      error : function() {
        if (page == 1) {
            $("#rows").empty();
        }
        alert("接口异常");
      }
    });
}

//循环列表
function loopRows(siteid,result)
{
    var html = '';
    for (var key in result) {
        if (result[key]['editable']) {
            var url = '/'+siteid+'/asset/edit?fn='+result[key]['name'];
            var target = ' target="_blank"';
        } else {
            var url = 'javascript:;';
            var target = '';
        }
        html += '<tr id="tr-'+result[key]['md5']+'"><th><div class="checkbox checkbox-primary m-r-15"><input name="ids" type="checkbox" value="'+result[key]['name']+'"><label for="checkbox2"></label></div></th><td>'+result[key]['name']+'</td><td>'+result[key]['type']+'</td><td>'+result[key]['size']+'</td><td>'+result[key]['friendlytime']+'</td><td><div class="btn-group"><a'+target+' href="'+url+'" class="btn btn-default btn-sm">编辑</a><a href="javascript:;" class="btn btn-default btn-sm delete-bt" data-siteid="'+siteid+'" data-id="'+result[key]['name']+'">删除</a></div></td></tr>';
    }
    return html;
}

